import 'package:bruno/bruno.dart';
import 'package:common/common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:miaowu/app/module/mine/audio_card/container_tab_indicator.dart';

import 'authentication_page.dart';
import 'integral_exchange_widget.dart';
import 'integral_histor_page.dart';

class IntegralHomePage extends StatefulWidget {
  const IntegralHomePage({super.key});

  @override
  State<IntegralHomePage> createState() => _IntegralHomePageState();
}

class _IntegralHomePageState extends State<IntegralHomePage> with TickerProviderStateMixin {
  late TabController _tabController;
  final List<Widget> _pages = [];

  late String category;

  final List<String> _tabs = ['全部', '支出', '收入'];

  @override
  void initState() {
    super.initState();
    _pages.add(const IntegralHistoryPage(inorout: '-1', key: PageStorageKey('全部')));
    _pages.add(const IntegralHistoryPage(inorout: '0', key: PageStorageKey('支出')));
    _pages.add(const IntegralHistoryPage(inorout: '1', key: PageStorageKey('收入')));
    _tabController = TabController(length: _pages.length, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Stack(
        children: [
          Image.asset('assets/app/images/member/integral_top_bg.png'),
          Scaffold(
            appBar: BrnAppBar(title: '积分', backgroundColor: Colors.transparent),
            backgroundColor: Colors.transparent,
            body: Container(
              color: Colors.transparent,
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 42.5.w),
                    margin: EdgeInsets.only(top: 20.h),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Text('609',
                                style: TextStyle(fontSize: 44.sp, color: Colors.black, fontWeight: FontWeight.bold)),
                            Container(
                              margin: const EdgeInsets.only(left: 20),
                              decoration: BoxDecoration(
                                  gradient: const LinearGradient(
                                      colors: [Color(0xFFEC4CD2), Color(0xFF548AFF)],
                                      begin: Alignment.centerLeft,
                                      end: Alignment.centerRight),
                                  borderRadius: BorderRadius.circular(25)),
                              padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 5),
                              child: Text('喵积分',
                                  style: TextStyle(color: Colors.white, fontSize: 12.sp, fontWeight: FontWeight.w600)),
                            ),
                          ],
                        ),
                        const SizedBox(height: 15),
                        Row(
                          children: [
                            Container(
                              height: 32.h,
                              decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(16.r)),
                              padding: const EdgeInsets.symmetric(horizontal: 12),
                              child: Center(
                                child: Text('兑换金币',
                                    style: TextStyle(
                                        color: const Color(0xFF25D4D0), fontSize: 12.sp, fontWeight: FontWeight.w600)),
                              ),
                            ).onTap(() {
                              SmartDialog.show(
                                usePenetrate: false,
                                clickMaskDismiss: true,
                                backType: SmartBackType.block,
                                keepSingle: true,
                                useAnimation: false,
                                builder: (context) => const Center(
                                  child: IntegralWxchangeWidget(),
                                ),
                              );
                            }),
                            Container(
                              margin: const EdgeInsets.only(left: 20),
                              height: 32.h,
                              decoration: BoxDecoration(
                                  color: const Color(0xFF25D4D0), borderRadius: BorderRadius.circular(16.r)),
                              padding: const EdgeInsets.symmetric(horizontal: 12),
                              child: Center(
                                child: Text('积分提现',
                                    style:
                                        TextStyle(color: Colors.white, fontSize: 12.sp, fontWeight: FontWeight.w600)),
                              ),
                            ).onTap(() {
                              Get.to(const AuthenticationPage());
                            }),
                          ],
                        ),
                      ],
                    ),
                  ),
                  _tabbarWarpper(),
                  Expanded(
                    child: TabBarView(
                        controller: _tabController,
                        children: _pages.map((e) {
                          return e;
                        }).toList()),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _tabbarWarpper() {
    return DefaultTabController(
      length: _pages.length,
      child: Container(
        // height: 40,
        margin: const EdgeInsets.only(top: 50),
        width: double.infinity,
        decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(topLeft: Radius.circular(16), topRight: Radius.circular(16))),
        child: TabBar(
          // tabAlignment: TabAlignment.start,
          controller: _tabController,
          labelColor: Colors.black,
          indicatorColor: Colors.black,
          unselectedLabelColor: Colors.black.withOpacity(0.56),
          // isScrollable: true,
          indicatorWeight: 0,
          // indicator: const UnderlineTabIndicator(
          //   borderSide: BorderSide(color: Colors.black, width: 5.0),
          //   insets: EdgeInsets.symmetric(horizontal: 10.0), // 设置指示器的水平间距
          // ),
          overlayColor: WidgetStateProperty.resolveWith((states) {
            return Colors.transparent;
          }),
          dividerHeight: 0,
          indicator: ContainerTabIndicator(
              color: const Color(0xFF22F2EB),
              width: 30.w,
              height: 3.w,
              radius: const BorderRadius.all(Radius.circular(14.0)),
              padding: const EdgeInsets.only(left: 0.0, right: 0, top: 15, bottom: 0)),
          labelStyle: const TextStyle(color: Color(0xFF000000), fontSize: 15, fontWeight: FontWeight.w500),
          unselectedLabelStyle: const TextStyle(color: Color(0xFF999999), fontSize: 15, fontWeight: FontWeight.w500),
          onTap: (index) {
            logs(index);
          },
          tabs: List.generate(_tabs.length, (index) {
            // return Tab(text: _tabs[index]);
            return Tab(child: Text(_tabs[index]));
          }),
        ),
      ),
    );
  }
}
